package com.admin.scroller.photoorvideo;

import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.admin.scroller.photoorvideo.adapter.PreviewAdapter;
import com.admin.scroller.photoorvideo.beans.LocalPhoto;

import java.util.ArrayList;
import java.util.List;

/***
 * 图片预览
 * 待完善，list grid 两种数据代入方式
 */
public class PerivewActivity extends AppCompatActivity {

    public static String FILE_PATH="filepath";
    public static String TYPE="type";
    public static String INDEX="index";
    public static String INDEX_PARENT="index_parent";

    private String filePath;

    private List<LocalPhoto> localPhotos=new ArrayList<>();
    private int index=0;
    private int type;

    private ViewPager viewPager;
    private PreviewAdapter previewAdapter;
    private ImageView ivPreBack;
    private ImageView ivPreSelected;
    private TextView tvPreSize;
    private LinearLayout linearSelected;
    private int size;
    private List<String> chosedD=new ArrayList<>();

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perview);

        filePath=getIntent().getStringExtra(FILE_PATH);
        type=getIntent().getIntExtra(TYPE,0);

        viewPager=findViewById(R.id.vp_preview);
        ivPreBack=findViewById(R.id.iv_pre_back);
        ivPreSelected=findViewById(R.id.iv_pre_selected);
        tvPreSize=findViewById(R.id.tv_pre_has_size);
        linearSelected=findViewById(R.id.linear_is_selected);

        previewAdapter=new PreviewAdapter(this);
        size=PhotoChoseActivity.ALLOW_SELECTED_SIZE;

        viewPager.setAdapter(previewAdapter);

        PhotoChoseActivity.viewModel.getChosedPhoto().observe(this, new Observer<List<String>>() {
            @Override
            public void onChanged(@Nullable List<String> strings) {
                chosedD=strings;
                if(strings==null){
                    tvPreSize.setText("0/"+size);
                }else {
                    tvPreSize.setText(strings.size()+"/"+size);
                }
            }
        });

        PhotoChoseActivity.viewModel.getLocalData().observe(this, new Observer<List<LocalPhoto>>() {
            @Override
            public void onChanged(@Nullable List<LocalPhoto> photos) {
                localPhotos.clear();
                localPhotos.addAll(photos);
                assert localPhotos != null;
                if(TextUtils.isEmpty(localPhotos.get(0).getPath())
                        && TextUtils.isEmpty(localPhotos.get(0).getVideoUrl())){
                    localPhotos.remove(0);
                }
                initData();
            }
        });

        initEvent();


    }

    private void initData(){
        if(type==0){//grid 列表传入的数据，index 准确
            index=getIntent().getIntExtra(INDEX,0);
        }else {
            //列表数据传入的数据，index 需要根据每一item的数量，根据parent与position计算
            for (int i = 0; i < localPhotos.size(); i++) {
                if (localPhotos.get(i).isVideo()) {
                    if (localPhotos.get(i).getVideoUrl().equals(filePath)) {
                        index = i;
                        break;
                    }
                } else {
                    if (localPhotos.get(i).getPath().equals(filePath)) {
                        index = i;
                        break;
                    }
                }
            }
        }
        previewAdapter.initAdapterData(localPhotos);
        viewPager.setCurrentItem(index,false);
        changeIsSelected(filePath,filePath);
    }

    private void changeSelested(boolean selected){
        if(selected){
            ivPreSelected.setImageResource(R.drawable.mis_btn_selected);
        }else {
            ivPreSelected.setImageResource(R.drawable.mis_btn_unselected);
        }
    }

    private void changeIsSelected(String path,String videoUrl){
        if(chosedD!=null){
            if(chosedD.contains(path)||chosedD.contains(videoUrl)){
                changeSelested(true);
                localPhotos.get(index).setSelected(true);
            }
        }
    }

    private void initEvent(){
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index=position;
                changeSelested(localPhotos.get(index).isSelected());
                changeIsSelected(localPhotos.get(index).getPath(),localPhotos.get(index).getVideoUrl());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        ivPreBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        linearSelected.setOnClickListener(clickListener);
    }

    private View.OnClickListener clickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean s=PhotoChoseActivity.viewModel.setChosePhotos(PerivewActivity.this,localPhotos.get(index).isVideo()?localPhotos.get(index).getVideoUrl():localPhotos.get(index).getPath());
            localPhotos.get(index).setSelected(s);
            changeSelested(s);
        }
    };

}
